package com.project.petLossSystem.controller.system;

import com.project.petLossSystem.common.CustomException;
import com.project.petLossSystem.service.AddressCategoryService;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import com.project.petLossSystem.vo.addressCategory.FirstAddressCategoryVO;
import com.project.petLossSystem.vo.addressCategory.SecondAddressCategoryVO;
import com.project.petLossSystem.vo.addressCategory.ThirdAddressCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SystemAddressCategoryController {
    @Autowired
    private AddressCategoryService addressCategoryService;

    @GetMapping("/firstAddressCategories")
    @ResponseBody
    public Result firstAddressCategories(){
        List<FirstAddressCategoryVO> firstAddresses = addressCategoryService.getAddressesForSelect();
        if(CollectionUtils.isEmpty(firstAddresses)){
            CustomException.failure("地址数据异常！");
        }
        return ResultGenerator.successResult(firstAddresses);
    }

    @GetMapping("/secondAddressCategories")
    @ResponseBody
    public Result secondAddressCategories(@RequestParam("addressId") Integer addressId){
        List<SecondAddressCategoryVO> secondAddresses = addressCategoryService.getSecondAddresses(addressId);
        if(CollectionUtils.isEmpty(secondAddresses)){
            return ResultGenerator.failureResult("无二级地址！");
        }
        return ResultGenerator.successResult(secondAddresses);
    }

    @GetMapping("/thirdAddressCategories")
    @ResponseBody
    public Result thirdAddressCategories(@RequestParam("addressId") Integer addressId){
        List<ThirdAddressCategoryVO> thirdAddresses = addressCategoryService.getThirdAddresses(addressId);
        if(CollectionUtils.isEmpty(thirdAddresses)){
            return ResultGenerator.failureResult("无三级地址！");
        }
        return ResultGenerator.successResult(thirdAddresses);
    }
}
